﻿using UnityEngine;
using UnityEngine.SceneManagement;
using System.Diagnostics;
using UnityEngine.UI;
using System.Collections;
using System.Xml;
using System.Collections.Generic;

public class xinPlaygroundScene : MonoBehaviour
{
    MediaPlayerCtrl m_Media;
    int movieID = 0;
    Stopwatch time = new Stopwatch();
    public int playTime;// The time of movie
    public string quitScene;// Quit for next scene

    // Use this for initialization
    void Start()
    {
        // Get m_Media of playing movies
        m_Media = GameObject.FindGameObjectWithTag("mSphere").GetComponent<MediaPlayerCtrl>();

        // Set start position of the photo sphere
        GameObject.Find("PhotoSphere").GetComponent<Transform>().localPosition = new Vector3(0, 100, 0);

        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("mSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, 0, 0);
            time.Start();
        }
        GameObject.Find("Button").GetComponent<Transform>().position = new Vector3(0, 100, 0.6f);
    }

    // Update is called once per frame
    void Update()
    {
        if (time.ElapsedMilliseconds >= (playTime + 3) * 1000)
        {
            time.Stop(); time.Reset();
            // Set the position of game objects after the end of the movie
            GameObject.Find("Button").GetComponent<Transform>().position = new Vector3(0, 0, 0.6f);
            GameObject.Find("MovieSphere").GetComponent<Transform>().localPosition = new Vector3(0, 100, 0);
            GameObject.Find("PhotoSphere").GetComponent<Transform>().localPosition = new Vector3(0, 0, 0);
        }
    }

    private void MovieChangeToPhoto()
    {
        // Change the position of sphere
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("pSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, 0, 0);
        }
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("mSphere"))
        {
            obj.GetComponent<Transform>().position = new Vector3(0, -100, 0);
        }
    }

    //加载视频
    public void RePlay()
    {
        m_Media.Load(m_Media.m_strFileName);
        m_Media.Play();
        time.Start();
        GameObject.Find("Button").GetComponent<Transform>().position = new Vector3(0, 100, 0.6f);
        GameObject.Find("MovieSphere").GetComponent<Transform>().localPosition = new Vector3(0, 0, 0);
        GameObject.Find("PhotoSphere").GetComponent<Transform>().localPosition = new Vector3(0, 100, 0);
    }

    // Quit this scene
    public void Quit()
    {
        SceneManager.LoadScene(quitScene);
    }
}
